# @Version        : 1.0
# @Update Time    : 2025/3/14 17:02
# @File           : favorites_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
import flet as ft
from flet_core import FontWeight, ImageFit, MainAxisAlignment


class FavoritesView(ft.Column):
    def __init__(self, page):
        super().__init__()
        self.bgcolor = "#f5f5f5"
        self.padding = 20
        self.page = page
        self.build_interface()

    def build_interface(self):
        self.controls = [
            ft.Container(
                content=ft.Row(
                    [
                        ft.Text("通知设置", size=20, weight=ft.FontWeight.BOLD)
                    ], alignment=ft.MainAxisAlignment.START
                ),
                padding=15,
                border=ft.border.only(bottom=ft.border.BorderSide(1, ft.colors.GREY_300))
            ),
            self.create_product_grid()
        ]
        # 设置垂直居中
        self.vertical_alignment = ft.MainAxisAlignment.CENTER

    def create_product_grid(self):
        return ft.GridView(
            expand=1,
            runs_count=2,
            max_extent=300,
            child_aspect_ratio=0.8,
            spacing=15,
            controls=self.create_product_cards()
        )

    def create_product_cards(self):
        products = [
            {
                "name": "芒果",
                "image": "assets/mango.png",
                "weight": "16.8kg",
                "exclusive": True
            },
            {
                "name": "葡萄",
                "image": "assets/grapes.png",
                "weight": "8.6kg",
                "exclusive": False
            },
            {
                "name": "草莓",
                "image": "assets/product1.png",
                "weight": "12.5mg",
                "exclusive": True
            },
            {
                "name": "牛油果",
                "image": "assets/avocado.png",
                "weight": "23.9kg",
                "exclusive": False
            }
        ]

        cards = []
        for product in products:
            card = ft.Card(
                elevation=3,
                content=ft.Container(
                    padding=10,  # 增加内边距以避免内容溢出
                    content=ft.Column(
                        [
                            ft.Stack(
                                [
                                    ft.Image(
                                        src=product["image"],
                                        height=120,  # 固定高度
                                        fit=ImageFit.CONTAIN  # 修改为CONTAIN以适应宽度
                                    ),
                                    ft.Container(
                                        content=ft.Text("独家", color="white", size=12),
                                        bgcolor="#d32f2f",
                                        padding=5,
                                        border_radius=3,
                                        visible=product["exclusive"],
                                        top=5,
                                        right=5,
                                    ) if product["exclusive"] else ft.Container()
                                ]
                            ),
                            ft.Column(  # 使用Column来包裹产品名称和价格
                                [
                                    ft.Row(
                                        [
                                            ft.Text(product["name"], size=16, weight=FontWeight.BOLD),
                                        ], alignment=MainAxisAlignment.START, expand=True
                                    ),
                                    ft.Row(
                                        [
                                            ft.Text(product["weight"], color="grey"),
                                        ], alignment=MainAxisAlignment.START, expand=True
                                    ),
                                ], spacing=5
                            ),
                            ft.Row(
                                [
                                    ft.IconButton(
                                        icon=ft.icons.DELETE,
                                        icon_color="#d32f2f",
                                        icon_size=20,
                                        on_click=lambda e: self.delete_item(f"{product['name']}删除成功")
                                    ),
                                    ft.IconButton(
                                        icon=ft.icons.ADD,
                                        icon_color="green",
                                        icon_size=20,
                                        on_click=lambda e: self.add_to_cart(f"{product['name']}添加购物车成功")
                                    )
                                ], alignment=MainAxisAlignment.CENTER  # 确保按钮靠右对齐
                            )
                        ], horizontal_alignment=ft.CrossAxisAlignment.CENTER  # 设置水平对齐方式
                    )
                )
            )
            cards.append(card)
        return cards

    def delete_item(self, msg):
        # 实现删除逻辑
        self.page.snack_bar = ft.SnackBar(
            ft.Text(msg),
            bgcolor=ft.colors.GREEN_500
        )
        self.page.snack_bar.open = True
        self.page.update()

    def add_to_cart(self, msg):
        # 实现添加到购物车逻辑
        self.page.snack_bar = ft.SnackBar(
            ft.Text(msg),
            bgcolor=ft.colors.GREEN_500
        )
        self.page.snack_bar.open = True
        self.page.update()